package com.boc.langxiang.ui.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.boc.langxiang.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Li Guoxi on 2016/6/28.
 */
public class SpecificationSelector extends LinearLayout {

    private Context mContext;

    private String[] mTabTitles;

    private int mCurrentTab = -1;

    private OnSpecificationSelectListener mOnSpecificationSelectListener;

    private List<Integer> mCannotSelectTAbs;

    public SpecificationSelector(Context context) {
        super(context);
        this.mContext = context;
        init();
    }

    public SpecificationSelector(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init();
    }

    public SpecificationSelector(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    private void init() {
        setOrientation(HORIZONTAL);
    }


    public void setTabTitles(String[] mTabTitles) {
        this.mTabTitles = mTabTitles;
        notifyDataSetChanged();
    }

    private void notifyDataSetChanged() {
        for (int i = 0; i < mTabTitles.length; i++) {
            String title = mTabTitles[i];
            View view = LayoutInflater.from(mContext).inflate(R.layout.specification_tab_layout, this, false);
            view.setTag(i);
            addTabs(title, i, view);
        }
    }

    private void addTabs(final String title, int i, View view) {
        TextView titleTv = (TextView) view.findViewById(R.id.tab_title);
        titleTv.setText(title);
        LayoutParams params = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.leftMargin = 8;
        params.rightMargin = 8;
        view.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = (int) v.getTag();
                if (mCurrentTab != position) {
                    if (mCannotSelectTAbs != null && mCannotSelectTAbs.contains(position)) {
                        mOnSpecificationSelectListener.onFailed(position, title);
                    } else {
                        setCurrentTab(position);
                        if (mOnSpecificationSelectListener != null) {
                            mOnSpecificationSelectListener.onSelected(position, title);
                        }
                    }
                }
            }
        });

        addView(view, i);
    }

    public void setCurrentTab(int position) {
        this.mCurrentTab = position;
        updateTabSelection(position);
    }

    private void updateTabSelection(int position) {
        for (int i = 0; i < mTabTitles.length; i++) {
            View view = getChildAt(i);
            boolean isSelected = (i == position);
            TextView textView = (TextView) view.findViewById(R.id.tab_title);
            if (isSelected) {
                view.findViewById(R.id.tab_container).setBackgroundResource(R.drawable.black_round_shape);
                textView.setTextColor(Color.WHITE);
            } else {
                view.findViewById(R.id.tab_container).setBackgroundColor(Color.TRANSPARENT);
                textView.setTextColor(Color.BLACK);
            }
        }
    }

    public OnSpecificationSelectListener getOnSpecificationSelectListener() {
        return mOnSpecificationSelectListener;
    }

    public void setOnSpecificationSelectListener(OnSpecificationSelectListener onSpecificationSelectListener) {
        this.mOnSpecificationSelectListener = onSpecificationSelectListener;
    }

    public void setCannotSelectTAbs(int... tabs) {
        mCannotSelectTAbs = new ArrayList<>();
        for (int i = 0; i < tabs.length; i++) {
            mCannotSelectTAbs.add(tabs[i]);
        }
    }

    public interface OnSpecificationSelectListener {
        void onSelected(int position, String title);

        void onFailed(int position, String title);
    }
}
